package com.zhao.service.serviceImpl;

import com.zhao.Others.FileOperationObject;
import com.zhao.dao.DepartmentDao;
import com.zhao.dao.MajorDao;
import com.zhao.dao.SchoolCounsellorDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.*;
import com.zhao.service.SchoolCounsellorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SchoolCounsellorServiceImpl implements SchoolCounsellorService {

    @Autowired
    private SchoolCounsellorDao schoolCounsellorDao;

    @Autowired
    private UniversityDao universityDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private MajorDao majorDao;


    @Override
    public String schoolCounsellorFinishInfo(SchoolCounsellor schoolCounsellor, MultipartFile image, HttpSession session) {

        String visitPath = FileOperationObject.savePicture(image,"userimage",schoolCounsellor.getUniversityID(),schoolCounsellor.getSchoolCounsellorID());

        schoolCounsellor.setImage(visitPath);
        schoolCounsellor.setIsRegisted("1");

        int res = schoolCounsellorDao.update(schoolCounsellor);
        if(res!=1){
            return "error";
        }
        System.out.println(schoolCounsellor);
        return "success";
    }

    @Override
    public String addOneSchoolCounsellor(SchoolCounsellor schoolCounsellor, HttpSession session) {

        SchoolCounsellor schoolCounsellor1 = schoolCounsellorDao.findByID(schoolCounsellor.getSchoolCounsellorID(),schoolCounsellor.getUniversityID());
        if(schoolCounsellor1!= null){
            return "ID已经存在,添加失败";
        }

        int res = schoolCounsellorDao.addOne(schoolCounsellor);
        if(res != 1){
            return "添加失败";
        }

        return "success";
    }

    private SchoolCounsellorImpl schoolCounsellorToSchoolCounsellorImpl(SchoolCounsellor schoolCounsellor){
        SchoolCounsellorImpl schoolCounsellor1 = new SchoolCounsellorImpl();

        University university = universityDao.findByID(schoolCounsellor.getUniversityID());
        schoolCounsellor1.setUniversityName(university.getUniversityName());
        schoolCounsellor1.setUniversityID(university.getUniversityID());

        Department department = departmentDao.findByID(schoolCounsellor.getDepartmentID(),schoolCounsellor.getUniversityID());
        schoolCounsellor1.setDepartmentID(department.getDepartmentID());
        schoolCounsellor1.setDepartmentName(department.getDepartmentName());

        Major major = majorDao.findByID(schoolCounsellor.getMajorID(),schoolCounsellor.getUniversityID());
        schoolCounsellor1.setMajorName(major.getMajorName());
        schoolCounsellor1.setMajorID(major.getMajorID());

        schoolCounsellor1.setPhoneNumber(schoolCounsellor.getPhoneNumber());
        schoolCounsellor1.setPassword(schoolCounsellor.getPassword());
        schoolCounsellor1.setIsRegisted(schoolCounsellor.getIsRegisted());
        schoolCounsellor1.setImage(schoolCounsellor.getImage());
        schoolCounsellor1.setGender(schoolCounsellor.getGender());
        schoolCounsellor1.setEmail(schoolCounsellor.getEmail());
        schoolCounsellor1.setName(schoolCounsellor.getName());
        schoolCounsellor1.setSchoolCounsellorID(schoolCounsellor.getSchoolCounsellorID());

        return schoolCounsellor1;

    }

    @Override
    public String getAll(String universityID, HttpSession session) {
        List<SchoolCounsellor> schoolCounsellorList = schoolCounsellorDao.findAll(universityID);

        List<SchoolCounsellorImpl> schoolCounsellorList1 = new ArrayList<>();
        for(SchoolCounsellor schoolCounsellor : schoolCounsellorList){
            schoolCounsellorList1.add(schoolCounsellorToSchoolCounsellorImpl(schoolCounsellor));
        }

        session.setAttribute("schoolCounsellorList",schoolCounsellorList1);
        return "success";
    }

    @Override
    public String adminGetOne(Admin admin, String schoolCounsellorID, HttpSession session) {

        SchoolCounsellor schoolCounsellor = schoolCounsellorDao.findByID(schoolCounsellorID,admin.getUniversityID());

        SchoolCounsellorImpl schoolCounsellor1 = schoolCounsellorToSchoolCounsellorImpl(schoolCounsellor);

        session.setAttribute("adminSeeschoolCounsellor",schoolCounsellor1);

        return "success";
    }

    @Override
    public String adminUpdateSchoolCounsellor(Admin admin, String schoolCounsellorID, String name, String department, String major, String password, String email, String phoneNumber, HttpSession session) {

        SchoolCounsellor schoolCounsellor = schoolCounsellorDao.findByID(schoolCounsellorID,admin.getUniversityID());
        if(schoolCounsellor == null){
            return "工号错误";
        }

        schoolCounsellor.setPassword(password);
        schoolCounsellor.setEmail(email);
        schoolCounsellor.setPhoneNumber(phoneNumber);
        schoolCounsellor.setName(name);

        Department department1 = departmentDao.findByName(department,admin.getUniversityID());
        if(department1 == null){
            return "学院名称错误";
        }
        schoolCounsellor.setDepartmentID(department1.getDepartmentID());

        Major major1 = majorDao.findByName(major,admin.getUniversityID());
        if(major1 == null){
            return "专业名称错误";
        }
        schoolCounsellor.setMajorID(major1.getMajorID());

        int res = schoolCounsellorDao.update(schoolCounsellor);
        if(res != 1){
            return "更新失败";
        }

        return "success";
    }
}
